package servlet;

import java.io.IOException;
import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import decoder.ChineseDecoder;

import bean.GoodsListBean;

import model.Goods;

import factory.DaoFactory;

/**
 * Servlet implementation class OrderGoodsServlet
 */
@WebServlet("/OrderGoodsServlet")
public class OrderGoodsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public OrderGoodsServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name = ChineseDecoder.getUtf8FromIso8859_1(request.getParameter("name"));
		String day = ChineseDecoder.getUtf8FromIso8859_1(request.getParameter("date"));
		Date date = new Date(Calendar.getInstance().getTimeInMillis());
		switch (day) {
		case "day1":
			date = new Date(Calendar.getInstance().getTimeInMillis());
			break;
		case "day2":
			date = new Date(Calendar.getInstance().getTimeInMillis() + 24*3600000L);
			break;
		case "day3":
			date = new Date(Calendar.getInstance().getTimeInMillis() + 2*24*3600000L);
			break;
		case "day4":
			date = new Date(Calendar.getInstance().getTimeInMillis() + 3*24*3600000L);
			break;
		case "day5":
			date = new Date(Calendar.getInstance().getTimeInMillis() + 4*24*3600000L);
			break;
		case "day6":
			date = new Date(Calendar.getInstance().getTimeInMillis() + 5*24*3600000L);
			break;
		case "day7":
			date = new Date(Calendar.getInstance().getTimeInMillis() + 6*24*3600000L);
			break;
		default:
			DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd");
			java.util.Date d = null;
			try {
				d = fmt.parse(day);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			date = new Date(d.getTime());
		}
		List<Goods> list = DaoFactory.getGoodsDao().find(date);
		GoodsListBean goodsList = new GoodsListBean(list);
		request.setAttribute("goodsList", goodsList);
		String address ="/jsp/OrderGoods.jsp?name=" + name + "&date=" + day;
		RequestDispatcher dispatcher =request.getRequestDispatcher(address);
		dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
